import threading
import uuid
import pyautogui
import sys
import mwindow
import datetime
import time
import sched
from pynput.keyboard import Controller, Key, Listener
import skillmanager
import win32gui
import win32con
from enum import Enum

class ADItem(Enum):
    ChaoZhi = 0
    PuTongBaoShi = 1
    CuiCanBaoShi = 2
    PuTongZhaopin = 3
    CuiCanZhaopin = 4
    JinBi = 5
    MiYan = 6
    GongFangTry = 7
def on_press(key):
    # try:
    #     print("正在按压:", format(key.char))
    # except AttributeError:
    #     print("正在按压2:", format(key))
    if(key.char == 'q'):
        return False
    
def DragMouse(x1:int,y1:int,x2:int,y2:int):
    pyautogui.mouseDown(x1, y1, button='left')
    pyautogui.moveTo(x2, y2)
    pyautogui.mouseUp(button='left') 

def CheckOpStateFinished(op_state:list)->bool:
    for s in op_state:
        if s == 0:
            return False
    return True
def CheckOpStateShangDianFinished(op_state:list)->bool:
    # for s in op_state[0:6]:
    #     if s == 0:
    #         return False
    # return True
    if op_state[ADItem.ChaoZhi.value] == 1 and op_state[ADItem.JinBi.value] == 1:
        return True
    return False

def DoShangDian(win:mwindow.MWindow, op_state:list):
    if CheckOpStateShangDianFinished(op_state):
        win.ClickLabel("城堡")
    else:
        if op_state[ADItem.ChaoZhi.value] == 0:
            poss = win.ExistLabel('超')
            if poss != None:
                win.ClickYAxisLabel("超",poss=poss)
        else:
            operated = False
            poss = win.ExistLabel('普通免费')
            if poss != None:
                win.ClickYAxisLabel("普通免费",poss=poss)
            poss = win.ExistLabel('璀璨免费')
            if poss != None:
                win.ClickYAxisLabel("璀璨免费",poss=poss)

            # if op_state[ADItem.PuTongBaoShi.value] == 0:
            #     poss = win.ExistLabel('普通免费')
            #     if poss != None:
            #         win.ClickYAxisLabel("普通免费",poss=poss)
            #         op_state[ADItem.PuTongBaoShi.value] = 1
            #         operated = True
            #     else:
            #         poss = win.ExistLabel('普通50')
            #         op_state[ADItem.PuTongBaoShi.value] = 1
            #         operated = True
            # elif op_state[ADItem.PuTongZhaopin.value] == 0:
            #     poss = win.ExistLabel('普通免费')
            #     if poss != None:
            #         win.ClickYAxisLabel("普通免费",poss=poss)
            #         op_state[ADItem.PuTongZhaopin.value] = 1
            #         operated = True
            #     else:
            #         poss = win.ExistLabel('普通50')
            #         if poss != None:
            #             op_state[ADItem.PuTongZhaopin.value] = 1
            #             operated = True
            # if not operated:
            #     if op_state[ADItem.CuiCanBaoShi.value] == 0:
            #         poss = win.ExistLabel('璀璨免费')
            #         if poss != None:
            #             win.ClickYAxisLabel("璀璨免费",poss=poss)
            #             op_state[ADItem.CuiCanBaoShi.value] = 1
            #         else:
            #             poss = win.ExistLabel('璀璨250')
            #             if poss != None:
            #                 op_state[ADItem.CuiCanBaoShi.value] = 1
            #     elif op_state[ADItem.CuiCanZhaopin.value] == 0:
            #         poss = win.ExistLabel('璀璨免费')
            #         if poss != None:
            #             win.ClickYAxisLabel("璀璨免费",poss=poss)
            #             op_state[ADItem.CuiCanZhaopin.value] = 1
            #         else:
            #             poss = win.ExistLabel('璀璨250')
            #             if poss != None:
            #                 op_state[ADItem.CuiCanZhaopin.value] = 1
            if not operated and op_state[ADItem.JinBi.value] == 0:
                poss = win.ExistLabel('金币免费')
                if poss != None:
                    win.ClickYAxisLabel("金币免费",poss=poss)
                else:
                    poss = win.ExistLabel('金币免费获取')
                    if poss != None:
                        win.ClickYAxisLabel("金币免费获取",poss=poss)
                        op_state[ADItem.JinBi.value] = 1
                    else:
                        poss = win.ExistLabel('金币50')
                        if poss != None:
                            op_state[ADItem.JinBi.value] = 1
        if op_state[ADItem.ChaoZhi.value] == 0:
            DragMouse(win.region[0] + 228, win.region[1] + 167,win.region[0] + 228, win.region[1] + 727)
        else:
            DragMouse(win.region[0] + 228, win.region[1] + 367,win.region[0] + 228, win.region[1] + 167)

def DoChengBao(win:mwindow.MWindow, op_state:list):
    if op_state[ADItem.MiYan.value] == 0:
        win.ClickLabel("法术书")
        win.ClickLabel("秘研")
    else:
        win.ClickLabel("工坊")
        poss = win.ExistLabel("领取")
        if poss != None:
            win.ClickYAxisLabel("领取",poss=poss)
        poss = win.ExistLabel("播放")
        if poss != None:
            win.ClickYAxisLabel("播放",poss=poss)
            op_state[ADItem.GongFangTry.value] += 1
        elif op_state[ADItem.GongFangTry.value] == 0:
            DragMouse(win.region[0] + 228, win.region[1] + 434,win.region[0] + 228, win.region[1] + 670)
        else:
            DragMouse(win.region[0] + 228, win.region[1] + 670,win.region[0] + 228, win.region[1] + 434)
def ontimer(mainwin,mset : mwindow.MWindowSet,op_state:list):
    print(f'ontimer: op_state={op_state}')
    if CheckOpStateFinished(op_state):
        print('所有工作完成')
        exit()
    win = mset.CheckWindow()
    if win != None:
        if win.name == "超值礼包":
            text = win.GetLabelText('每日福利状态')
            if text == '已售罄':
                win.ClickLabel("退出")
            elif text == '兔费' or text == '免费':
                win.ClickLabel("每日福利状态")
                win.ClickLabel("点击屏幕继续")
            op_state[ADItem.ChaoZhi.value] = 1
        elif win.name == "广告":
            text = win.GetLabelText('已获得奖励')
            if text in ['已获得奖励','已获得奖状','已获得奖成']:
                win.ClickLabel("关闭")
        elif win.name == "广告奖励":
            win.ClickLabel("确定")
        elif win.name == "秘研":
            if(win.ExistLabel("获取秘卷")):
                win.ClickLabel("获取秘卷")
            else:
                win.ClickLabel("界面外")
            op_state[ADItem.MiYan.value] = 1
        elif win.name == "主界面-商店":
            DoShangDian(win,op_state)
        elif win.name == "主界面-城堡":
            DoChengBao(win,op_state)
        else:
            win : mwindow.MWindow = mset.mwindows[0] 
            win.ClickLabel("商店")
    else:
        print("未检测到窗体")
    loop_monitor(op_state)
def loop_monitor(op_state):
    timer=threading.Timer(3,ontimer,args=[mainwin,mset,op_state])
    timer.start()    

def GetMainWnd():
    hwnd = win32gui.FindWindow('Chrome_WidgetWin_0', '国王指意')    
    if hwnd == None:
        return False
    windows = pyautogui.getWindowsWithTitle("国王指意")
    mainwin = None
    for w in windows:
        if w._hWnd == hwnd:
            mainwin = w
            break
    return mainwin

if __name__ == "__main__":
    pyautogui.PAUSE = 2.5
    pyautogui.FAILSAFE = True
    op_state = [0,0,0,0,0,0,0,0]
    print("start")
    mainwin = GetMainWnd()
    if mainwin == None:
        print("没有找到")
        sys.exit(0)
    mainwin.move(0,0)
    # win32gui.BringWindowToTop(hwnd)
    mainwin.activate()
    # print("6")
    if mainwin.box.left < -32000:
        print("窗口小于-30000")
        sys.exit(0)

    # 初始化窗口集合
    mset = mwindow.MWindowSet((mainwin.box.left,mainwin.box.top,mainwin.box.width,mainwin.box.height))
    mset.Load('看广告.mset')

    # with Listener(on_press=on_press) as listener:
    #     listener.join()        
    loop_monitor(op_state)
